package com.example.demo.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.pojo.Tongzhi;
import com.example.demo.service.ITongzhiService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Tongzhi")
public class TongzhiController {

	@Resource
	private ITongzhiService TongzhiService;

	@RequestMapping("/selectTongzhiByTitle")
	public void selectTongzhiByTitle(HttpServletResponse response, HttpServletRequest request, String title)
			throws IOException {
		List<Tongzhi> list = TongzhiService.selectTongzhiByTitle(title);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectTongzhiByCommunity")
	public void selectTongzhiByCommunity(HttpServletResponse response, HttpServletRequest request, String community)
			throws IOException {
		List<Tongzhi> list = TongzhiService.selectTongzhiByCommunity(community);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectTongzhi")
	public void selectTongzhi(HttpServletResponse response, HttpServletRequest request, String openid)
			throws IOException {
		List<Tongzhi> list = TongzhiService.selectTongzhi();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectTongzhiById")
	public void selectTongzhiById(HttpServletResponse response, HttpServletRequest request, Integer id)
			throws IOException {
		Gson gson = new Gson();
		String json = gson.toJson(TongzhiService.selectByPrimaryKey(id));
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

}
